package com.sky.orangehireserver.modules.enterprise.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.orangehireserver.aspect.ApiOperationLog;
import com.sky.orangehireserver.modules.enterprise.dto.EnterpriseEmployeeCreateDTO;
import com.sky.orangehireserver.modules.enterprise.dto.EnterpriseEmployeePasswordResetDTO;
import com.sky.orangehireserver.modules.enterprise.dto.EnterpriseEmployeeQueryDTO;
import com.sky.orangehireserver.modules.enterprise.dto.EnterpriseEmployeeUpdateDTO;
import com.sky.orangehireserver.modules.enterprise.service.EnterpriseAccountService;
import com.sky.orangehireserver.modules.enterprise.vo.EnterpriseEmployeeVO;
import com.sky.orangehireserver.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业账户管理控制器
 * 
 * @author sky
 */
@Slf4j
@RestController
@RequestMapping("/enterprise/account")
@RequiredArgsConstructor
@Tag(name = "企业账户管理", description = "企业管理员和HR管理企业员工账户相关接口")
public class EnterpriseAccountController {

    private final EnterpriseAccountService enterpriseAccountService;

    /**
     * 创建企业员工
     * 
     * @param orgId 企业ID（从请求头或上下文中获取）
     * @param currentUserRole 当前用户角色（从请求头或上下文中获取）
     * @param createDTO 员工创建请求DTO
     * @return 员工信息VO
     */
    @PostMapping
    @Operation(summary = "创建企业员工", description = "企业管理员创建HR，HR创建其他员工")
    @ApiOperationLog(description = "创建企业员工")
    public Response<EnterpriseEmployeeVO> createEmployee(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole,
            @Valid @RequestBody EnterpriseEmployeeCreateDTO createDTO) {
        
        // TODO: 在实际项目中，企业ID和用户角色应该从JWT token或用户上下文中获取
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        EnterpriseEmployeeVO employeeVO = enterpriseAccountService.createEmployee(orgId, currentUserRole, createDTO);
        return Response.success(employeeVO);
    }

    /**
     * 更新企业员工信息
     * 
     * @param orgId 企业ID
     * @param currentUserRole 当前用户角色
     * @param updateDTO 员工更新请求DTO
     * @return 员工信息VO
     */
    @PutMapping
    @Operation(summary = "更新企业员工信息", description = "更新企业员工信息")
    @ApiOperationLog(description = "更新企业员工信息")
    public Response<EnterpriseEmployeeVO> updateEmployee(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole,
            @Valid @RequestBody EnterpriseEmployeeUpdateDTO updateDTO) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        EnterpriseEmployeeVO employeeVO = enterpriseAccountService.updateEmployee(orgId, currentUserRole, updateDTO);
        return Response.success(employeeVO);
    }

    /**
     * 删除企业员工
     * 
     * @param orgId 企业ID
     * @param currentUserRole 当前用户角色
     * @param employeeId 员工ID
     * @return 操作结果
     */
    @DeleteMapping("/{employeeId}")
    @Operation(summary = "删除企业员工", description = "删除企业员工")
    @ApiOperationLog(description = "删除企业员工")
    public Response<Void> deleteEmployee(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole,
            @Parameter(description = "员工ID") @PathVariable Long employeeId) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        enterpriseAccountService.deleteEmployee(orgId, currentUserRole, employeeId);
        return Response.success();
    }

    /**
     * 获取企业员工详情
     * 
     * @param orgId 企业ID
     * @param employeeId 员工ID
     * @return 员工信息VO
     */
    @GetMapping("/{employeeId}")
    @Operation(summary = "获取企业员工详情", description = "根据ID获取企业员工详细信息")
    @ApiOperationLog(description = "获取企业员工详情")
    public Response<EnterpriseEmployeeVO> getEmployeeById(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "员工ID") @PathVariable Long employeeId) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        EnterpriseEmployeeVO employeeVO = enterpriseAccountService.getEmployeeById(orgId, employeeId);
        return Response.success(employeeVO);
    }

    /**
     * 分页查询企业员工列表
     * 
     * @param orgId 企业ID
     * @param currentUserRole 当前用户角色
     * @param queryDTO 查询条件DTO
     * @return 分页员工信息
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询企业员工列表", description = "根据条件分页查询企业员工列表")
    @ApiOperationLog(description = "分页查询企业员工列表")
    public Response<IPage<EnterpriseEmployeeVO>> getEmployeePage(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole,
            @Valid EnterpriseEmployeeQueryDTO queryDTO) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        IPage<EnterpriseEmployeeVO> employeePage = enterpriseAccountService.getEmployeePage(orgId, currentUserRole, queryDTO);
        return Response.success(employeePage);
    }

    /**
     * 启用企业员工
     * 
     * @param orgId 企业ID
     * @param currentUserRole 当前用户角色
     * @param employeeId 员工ID
     * @return 操作结果
     */
    @PutMapping("/{employeeId}/enable")
    @Operation(summary = "启用企业员工", description = "启用指定企业员工")
    @ApiOperationLog(description = "启用企业员工")
    public Response<Void> enableEmployee(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole,
            @Parameter(description = "员工ID") @PathVariable Long employeeId) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        enterpriseAccountService.enableEmployee(orgId, currentUserRole, employeeId);
        return Response.success();
    }

    /**
     * 禁用企业员工
     * 
     * @param orgId 企业ID
     * @param currentUserRole 当前用户角色
     * @param employeeId 员工ID
     * @return 操作结果
     */
    @PutMapping("/{employeeId}/disable")
    @Operation(summary = "禁用企业员工", description = "禁用指定企业员工")
    @ApiOperationLog(description = "禁用企业员工")
    public Response<Void> disableEmployee(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole,
            @Parameter(description = "员工ID") @PathVariable Long employeeId) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        enterpriseAccountService.disableEmployee(orgId, currentUserRole, employeeId);
        return Response.success();
    }

    /**
     * 重置企业员工密码
     * 
     * @param orgId 企业ID
     * @param currentUserRole 当前用户角色
     * @param resetDTO 密码重置请求DTO
     * @return 操作结果
     */
    @PutMapping("/reset-password")
    @Operation(summary = "重置企业员工密码", description = "重置企业员工密码")
    @ApiOperationLog(description = "重置企业员工密码")
    public Response<Void> resetEmployeePassword(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole,
            @Valid @RequestBody EnterpriseEmployeePasswordResetDTO resetDTO) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        enterpriseAccountService.resetEmployeePassword(orgId, currentUserRole, resetDTO);
        return Response.success();
    }

    /**
     * 批量删除企业员工
     * 
     * @param orgId 企业ID
     * @param currentUserRole 当前用户角色
     * @param employeeIds 员工ID列表
     * @return 操作结果
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除企业员工", description = "批量删除多个企业员工")
    @ApiOperationLog(description = "批量删除企业员工")
    public Response<Void> batchDeleteEmployees(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole,
            @Parameter(description = "员工ID列表") @RequestBody List<Long> employeeIds) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        enterpriseAccountService.batchDeleteEmployees(orgId, currentUserRole, employeeIds);
        return Response.success();
    }

    /**
     * 获取当前用户可管理的角色列表
     * 
     * @param currentUserRole 当前用户角色
     * @return 可管理的角色列表
     */
    @GetMapping("/manageable-roles")
    @Operation(summary = "获取可管理的角色列表", description = "获取当前用户可管理的角色列表")
    @ApiOperationLog(description = "获取可管理的角色列表")
    public Response<List<String>> getManageableRoles(
            @Parameter(description = "当前用户角色", hidden = true) @RequestHeader(value = "X-User-Role", required = false) String currentUserRole) {
        
        if (currentUserRole == null) {
            throw new RuntimeException("用户角色不能为空，请在请求头中添加X-User-Role");
        }
        
        List<String> manageableRoles = enterpriseAccountService.getManageableRoles(currentUserRole);
        return Response.success(manageableRoles);
    }
}
